import signinTpl from '../views/signin.art';
import {
  signinModel
} from '../models/signin';

//登录页面
const signin = (router) => {
  return (req, res, next) => {
    res.render(signinTpl());
    //绑定单击事件 并通过柯里化函数传参
    $('#signin').on('submit', _handleSubmit(router));
  };
};

//登录提交
const _handleSubmit = (router) => {
  return async (e) => {
    e.preventDefault();
    // router.go('/index');
    //提交表单
    const data = $('#signin').serialize();
    let {
      jqXHR,
      res
    } = await signinModel(data);
    const token = jqXHR.getResponseHeader('X-Access-Token');
    //token存入本地
    localStorage.setItem('lg-token', token);
    if (res.ret) {
      router.go('/index');
    }
  }
}

export default signin;